#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "C:\Codefile\CODE_C\C_Multiple\ContactBook\Header Files\BasicOperat.h"

void printPerson(Person PtrP);
void addPerson(Person PtrP);
void findPerson(Person PtrP);
void changePerson(Person PtrP);
void delPerson(Person PtrP);
void displayContact(Person PtrP);

void printPerson(Person PtrP)
{
    printf("Name:%s\n",PtrP->m_Person.Name);
    printf("Phone number:%s\n",PtrP->m_Person.PhoneNum);
}
void addPerson(Person PtrP)
{
    struct LNode p;
    printf ("Please enter his/her name:");
    scanf ("%s",p.m_Person.Name);
    printf ("Please enter his/her phone number:");
    scanf ("%s",p.m_Person.PhoneNum);
    p.m_Person.NameLast = strlen(p.m_Person.Name) - 1;
    p.m_Person.PhoNumLast = strlen(p.m_Person.PhoneNum) - 1;
    Insert(p,Length(PtrP),PtrP);
}
void findPerson(Person PtrP)
{
    struct ContactPerson *target;
    char search[40];
    printf("Please enter the name of the person you want to search for:");
    scanf("%s",search);
    target = PtrP;
    while (search!=target->name && target!=NULL)
    {
        target=target->next;
    }
    if(target==NULL)
    {
        printf("No matches.");
    }
    else
    {
        printPerson(target);
    }
}
void changePerson(Person PtrP)
{
    struct ContactPerson *target;
    char search[40];
    printf("Please enter the name of the person you want to change its details:");
    scanf("%s",search);
    target=PtrP;
    while (search!=target->name && target!=NULL)
    {
        target=target->next;
    }
    if(target==NULL)
    {
        printf("No matches.");
    }
    else
    {
        printPerson(target);
    }
    printf ("Please reenter his/her name:");
    scanf ("%s",PtrP->name);
    printf ("Please reenter his/her phone number:");
    scanf ("%s",PtrP->num);
    printf("Saved.");
}
void delPerson(Person PtrP)
{
    Person DelPerson = PtrP;
    PtrP = Delete(i,PtrP);
}

int main (void)
{
    char option;
    struct ContactPerson *head,*acp/*,*previous,*current*/;
    head=NULL;
    while(1)
    {
        printf("This is your e-contact.\n");
        printf("a.Add new contact person.\n");
        printf("b.Search former contact person.\n");
        printf("c.Change former contact person.\n");
        printf("d.Delete former contact person.\n");
        printf("e.Display my e-contact.\n");
        printf("f.Exit my e-contact.\n");
        printf("Please enter an order letter to opt the function above you need: ");
        scanf("%s",&option);
        if(option=='f')
        {
            printf("You have exited your Contact.");
            break;
        } 
        if(option=='a')
        {
            acp=(struct ContactPerson *)malloc(sizeof (struct ContactPerson));
            if(acp==NULL)
            {
                printf("Failed to allocate memory");
                exit(1);
            }
            else
            {
                addPerson(acp);
            }
            if(head==NULL)
            {
                head=acp;
            }
            else
            {
                /*current=head;
                previous=NULL;
                while((current->num)!=) ������һ�������Ž�������ɣ��������������ѯ����*/
                acp->next=head;
            }
            printf("==================Enter finished.==================\n");
            printPerson(acp);
            free(acp);
        }
        if(option=='b')
        {
            findPerson(head);
        }
        if(option=='c')
        {
            changePerson(head);
            printf("Details changed.");
        }
        if(option=='d')
        {
            delPerson(head);
            printf("Deletion Fininshed.");
        }
        if(option=='e')
        {
            acp=head;
            while(acp!=NULL)
            {
                printf("This is your contact:\n");
                printPerson(acp);
                acp=acp->next;
            }
        }
        printf("Please press ENTER to continue...\n");
        getchar();
        getchar();
    }
    return 0;
}